/**
 * 复制文本信息到剪切板
 */
export function copyText(
  text: string,
  success?: () => void,
  fail?: (res: string) => void
) {
  text = text.replace(/(^\s*)|(\s*$)/g, "");
  if (!text) {
    fail && fail("复制的内容不能为空！");
    return;
  }
  const id = "the-clipboard-for-copy";
  let clipboard = document.getElementById(id) as HTMLTextAreaElement;
  if (!clipboard) {
    clipboard = document.createElement("textarea");
    clipboard.id = id;
    clipboard.readOnly = true;
    clipboard.style.cssText =
      "font-size: 15px; position: fixed; top: -1000%; left: -1000%;";
    document.body.appendChild(clipboard);
  }
  clipboard.value = text;
  clipboard.select();
  clipboard.setSelectionRange(0, clipboard.value.length);
  const state = document.execCommand("copy");
  if (state) {
    success && success();
  } else {
    fail && fail("复制失败");
  }
}

/**
 * a标签打开链接
 * @param link
 * @param target
 */
export function openLink(
  link: string,
  target: "_blank" | "_self" | "_parent" | "_top" = "_blank"
) {
  const label = document.createElement("a");
  label.href = link;
  label.target = target;
  document.body.appendChild(label);
  label.click();
  document.body.removeChild(label);
}
